<?php

namespace app\admin\controller\information;

use app\common\controller\Backend;
use fast\Tree;
/**
 * Oa用户档案管理
 *
 * @icon fa fa-circle-o
 */
class Oauser extends Backend
{
    /**
     * U8user模型对象
     * @var \app\admin\model\information\Oauser
     */
    protected $model = null;
    protected $oadptModel = null;
    protected $allUpdate =[];
    protected $searchFields = "no,user_name,dept_id,dept_name,user_name_py,user_name_zm";
    protected $noNeedLogin = ['index','getTreeData'];
    
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\information\Oauser;
        $this->oadptModel = new \app\admin\model\information\Oadpt;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
    }
    
    public function index()
    {
        if (false === $this->request->isAjax()) {
            $this->assign('backoa',config('site.backoa'));
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();
        
        $list = $this->model
        ->where($where)
        ->order($sort, $order)
        ->paginate($limit);
        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }
    
    public function getTreeData(){
        
        $data = $this->oadptModel->field('dept_id id,dept_name name,parent_id pid')->select();
        
        $classtree = new Tree();
        $classtree->init($data);
        $treeList = $classtree->getTreeList($classtree->getTreeArray(0), 'name');
        
        foreach ($treeList as $k => $v) {
            $jsList[] = array(
                'id' => $v['id'],
                'parent' => $v['pid'] ? $v['pid'] : '#',
                'text' => __($v['name']),
            );
        }
        $this->success('','',$jsList);
    }
}
